home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / TURB_VIS / TVISION / CALENDAR.PAS < prev    next >
Pascal/Delphi Source File  |  1990-10-23  |  6KB  |  257 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal 6.0                             }
  4. {   Turbo Vision Demo                            }
  5. {   Copyright (c) 1990 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit Calendar;
  10.  
  11. {$F+,O+,X+,S-,D-}
  12.  
  13. { Calendar object for viewing a month at a time. See TVDEMO.PAS
  14.   for an example program that uses this unit.
  15. }
  16.  
  17. interface
  18.  
  19. uses Drivers, Objects, App, Views, Dos, Dialogs;
  20.  
  21. const
  22.  
  23.    DaysInMonth: array[1..12] of Byte =
  24.      (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  25.  
  26.    MonthStr: array[1..12] of string[10] =
  27.      ('January   ',
  28.       'February  ',
  29.       'March     ',
  30.       'April     ',
  31.       'May       ',
  32.       'June      ',
  33.       'July      ',
  34.       'August    ',
  35.       'September ',
  36.       'October   ',
  37.       'November  ',
  38.       'December  ');
  39.  
  40. type
  41.  
  42.  PCalendarView = ^TCalendarView;
  43.  TCalendarView = object(TView)
  44.    Year, Month, Days: Word;
  45.    CurYear, CurMonth, CurDay : Word;
  46.    constructor Init(Bounds: TRect);
  47.    constructor Load(var S: TStream);
  48.    procedure HandleEvent(var Event: TEvent); virtual;
  49.    procedure Draw; virtual;
  50.    procedure Store(var S: TStream);
  51.  end;
  52.  
  53.  PCalendarWindow = ^TCalendarWindow;
  54.  TCalendarWindow = object(TWindow)
  55.    constructor Init;
  56.  end;
  57.  
  58. const
  59.  
  60.   RCalendarView: TStreamRec = (
  61.      ObjType: 10020;
  62.      VmtLink: Ofs(TypeOf(TCalendarView)^);
  63.      Load:    @TCalendarView.Load;
  64.      Store:   @TCalendarView.Store
  65.   );
  66.   RCalendarWindow: TStreamRec = (
  67.      ObjType: 10021;
  68.      VmtLink: Ofs(TypeOf(TCalendarWindow)^);
  69.      Load:    @TCalendarWindow.Load;
  70.      Store:   @TCalendarWindow.Store
  71.   );
  72.  
  73. procedure RegisterCalendar;
  74.  
  75. implementation
  76.  
  77. { TCalendarWindow }
  78. constructor TCalendarWindow.Init;
  79. var
  80.   R:TRect;
  81. begin
  82.   R.Assign(1, 1, 23, 11);
  83.   TWindow.Init(R, 'Calendar', 0);
  84.   Flags := Flags and not (wfZoom + wfGrow);    { Not resizeable }
  85.   GrowMode :=0;
  86.   Palette := wpCyanWindow;
  87.  
  88.   GetExtent(R);
  89.   R.Grow(-1, -1);
  90.   Insert(New(PCalendarView, Init(R)));
  91. end;
  92.  
  93. { TCalendarView }
  94. constructor TCalendarView.Init(Bounds: TRect);
  95. var
  96.   H: Word;
  97. begin
  98.   TView.Init(Bounds);
  99.   Options := Options or ofSelectable;
  100.   EventMask := EventMask or evMouseAuto;
  101.   GetDate(CurYear, CurMonth, CurDay, H);
  102.   Year := CurYear;
  103.   Month := CurMonth;
  104.   DrawView;
  105. end;
  106.  
  107. constructor TCalendarView.Load(var S: TStream);
  108. var
  109.   H: Word;
  110. begin
  111.   TView.Load(S);
  112.   GetDate(CurYear, CurMonth, CurDay, H);
  113.   S.Read(Year, SizeOf(Year));
  114.   S.Read(Month, SizeOf(Month));
  115. end;
  116.  
  117. function DayOfWeek(Day, Month, Year: Integer) : Integer;
  118. var
  119.   century, yr, dw: Integer;
  120. begin
  121.   if Month < 3 then
  122.   begin
  123.     Inc(Month, 10);
  124.     Dec(Year);
  125.   end
  126.   else
  127.      Dec(Month, 2);
  128.   century := Year div 100;
  129.   yr := year mod 100;
  130.   dw := (((26 * month - 2) div 10) + day + yr + (yr div 4) +
  131.     (century div 4) - (2 * century)) mod 7;
  132.   if dw < 0 then DayOfWeek := dw + 7
  133.   else DayOfWeek := dw;
  134. end;
  135.  
  136. procedure TCalendarView.Draw;
  137. const
  138.   Width = 20;
  139. var
  140.   i, j, DayOf, CurDays: Integer;
  141.   S: String;
  142.   B: array[0..Width] of Word;
  143.   Color, BoldColor, SpecialColor: Byte;
  144.  
  145. function Num2Str(I: Integer): String;
  146. var
  147.   S:String;
  148. begin
  149.   Str(i:2, S);
  150.   Num2Str := S;
  151. end;
  152.  
  153. begin
  154.   Color:= GetColor(6);
  155.   BoldColor:= GetColor(7);
  156.   DayOf := DayOfWeek(1, Month, Year);
  157.   Days := DaysInMonth[Month] + Byte((Year mod 4 = 0) and (Month = 2));
  158.   Str(Year:4, S);
  159.   MoveChar(B, ' ', Color, Width);
  160.   MoveStr(B, MonthStr[Month] + S+' '#30'  '#31, Color);
  161.   WriteLine(0, 0, Width, 1, B);
  162.   MoveChar(B, ' ', Color, Width);
  163.   MoveStr(B, 'Su Mo Tu We Th Fr Sa', Color);
  164.   WriteLine(0, 1, Width, 1, B);
  165.   CurDays := 1 - DayOf;
  166.   for i := 1 to 6 do
  167.   begin
  168.     MoveChar(B, ' ', Color, Width);
  169.     for j := 0 to 6 do
  170.     begin
  171.       if (CurDays < 1) or (CurDays > Days) then
  172.         MoveStr(B[J * 3], '   ', Color)
  173.       else
  174.         { if it is the current day }
  175.         if (Year = CurYear) and (Month = CurMonth) and
  176.           (CurDays = CurDay) then
  177.           MoveStr(B[J * 3], Num2Str(CurDays), BoldColor)
  178.         else
  179.           MoveStr(B[J * 3], Num2Str(CurDays), Color);
  180.       Inc(CurDays);
  181.     end;
  182.     WriteLine(0, i + 1, Width, 1, B);
  183.   end;
  184. end;
  185.  
  186. procedure TCalendarView.HandleEvent(var Event: TEvent);
  187. var
  188.   Point:TPoint;
  189.   SelectDay: Word;
  190. begin
  191.   TView.HandleEvent(Event);
  192.   if (State and sfSelected <> 0) then
  193.   begin
  194.     if Event.What and (evMouseDown + evMouseAuto) <> 0 then
  195.     begin
  196.       MakeLocal(Event.Where, Point);
  197.       if ((Point.X = 15) and (Point.Y = 0)) then
  198.       begin
  199.         Inc(Month);
  200.         if Month > 12 then
  201.         begin
  202.           Inc(Year);
  203.           Month := 1;
  204.         end;
  205.         DrawView;
  206.       end;
  207.       if ((Point.X = 18) and (Point.Y = 0)) then
  208.       begin
  209.         Dec(Month);
  210.         if Month < 1 then
  211.         begin
  212.           Dec(Year);
  213.           Month := 12;
  214.         end;
  215.         DrawView;
  216.       end;
  217.     end
  218.     else if Event.What = evKeyDown then
  219.     begin
  220.       if (Lo(Event.KeyCode) = byte('+')) or (Event.KeyCode = kbDown) then
  221.       begin
  222.         Inc(Month);
  223.         if Month > 12 then
  224.         begin
  225.           Inc(Year);
  226.           Month := 1;
  227.         end;
  228.       end;
  229.       if (Lo(Event.KeyCode) = Byte('-')) or (Event.KeyCode = kbUp) then
  230.       begin
  231.         Dec(Month);
  232.         if Month < 1 then
  233.         begin
  234.           Dec(Year);
  235.           Month := 12;
  236.         end;
  237.       end;
  238.       DrawView;
  239.     end;
  240.   end;
  241. end;
  242.  
  243. procedure TCalendarView.Store(var S: TStream);
  244. begin
  245.   TView.Store(S);
  246.   S.Write(Year, SizeOf(Year));
  247.   S.Write(Month, SizeOf(Month));
  248. end;
  249.  
  250. procedure RegisterCalendar;
  251. begin
  252.   RegisterType(RCalendarView);
  253.   RegisterType(RCalendarWindow);
  254. end;
  255.  
  256. end.
  257.